/*
*-air address
*http://b-side.tistory.com/attachment/cfile24.uf@15079D0B49D9EB7E999854.air
*
*- publisher id
*7B5F962EECEFB4DDB2483E6A5F9F92FC7FBAB985.1
*
*- application id
*Test-AirbyFlex
*
*
*/

package {
	import clip.Btn;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;

//	import fl.controls.Button;

	public class Test_airBadge extends Sprite
	{
		//appliccation data
		private var app_path:String = "http://b-side.tistory.com/attachment/cfile22.uf@17357E0E49DB62F52CAA19.air";
		private var app_ID:String = "Test-AirbyFlex";
		private var pub_ID:String = "7B5F962EECEFB4DDB2483E6A5F9F92FC7FBAB985.1";
		private var airruntime_version:String = "1.5";
		
		//air swf data
		private var air_url:String = "http://airdownload.adobe.com/air/browserapi/air.swf";
		private var air:Object;
		private var loader:Loader;
		
		//on stage
		public var massage:TextField;
		public var btn:Btn;
		
		public function Test_airBadge()
		{
			btn.alpha = .5;
			loadAIR();
		}
		
		private function loadAIR():void
		{
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain = ApplicationDomain.currentDomain;

			loader = new Loader();
			loader.contentLoaderInfo.addEventListener( Event.INIT, initHandler );
			loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, errorHandler );
			loader.load( new URLRequest( air_url ), context)
			
		}		
		
		private function initHandler( evt:Event ):void
		{
			air = loader.content;
			
			switch( air.getStatus())
			{
				case "installed"://에어가 설치되어있는 경우
					massage.text = "어플을 깔아보자."
					air.getApplicationVersion( app_ID, pub_ID, getVersion );
				break;	
				case "available"://에어가 설치되어있지 않은 경우
					massage.text = "달이 차오른다~ 깔자. "
					revUpBtn("AIR install", false);
				break;
				case "unavailable"://에어를 설치할 수 없는 경우 
					massage.text = "님꺼는 구려서 AIR 못씀.";
				break;
			}
		}
		private function errorHandler( evt:IOErrorEvent ):void
		{
			massage.text = "미션실패!!!"
		}
		
		private function getVersion( version:String) :void
		{
//			trace(version)
			if( version == null )
			{
				massage.text = "press install";
				revUpBtn("install", false);
			}
			else 
			{
				massage.text = "실행합니다";
				revUpBtn("run", true);
			}
		}
		
		private function revUpBtn( string:String , app_installed:Boolean ):void
		{
			btn.alpha = 1;
			btn.buttonMode = true;
			btn.txt.text = string;
			
			if(app_installed)
				btn.addEventListener(MouseEvent.MOUSE_DOWN, runApplication );
			else 
				btn.addEventListener(MouseEvent.MOUSE_DOWN, installApplication );
		}
		
		private function runApplication( evt:MouseEvent ):void
		{
			air.launchApplication( app_ID, pub_ID );
		} 
		private function installApplication( evt:MouseEvent ):void
		{
			air.installApplication( app_path, airruntime_version );
		}
	}
}
